//
//  PRSystem.m
//  CandyJump
//
//  Created by Bon Lam on 05/03/2011.
//  Copyright 2011 Peru Republic. All rights reserved.
//

#import "PRDevice.h"


//=====================================================================================================================
@implementation UIDevice(machine)

//---------------------------------------------------------------------------------------------------------------------
- (NSString *)machine
{
	size_t size;
	// Set 'oldp' parameter to NULL to get the size of the data
	// returned so we can allocate appropriate amount of space
	sysctlbyname("hw.machine", NULL, &size, NULL, 0); 
	
	// Allocate the space to store name
	char *name = malloc(size);
	
	// Get the platform name
	sysctlbyname("hw.machine", name, &size, NULL, 0);
	
	// Place name into a string
	NSString *machine = [NSString stringWithCString:name encoding:NSUnicodeStringEncoding];
	
	// Done with this
	free(name);
	
	return machine;
}

@end

#pragma mark -
//=====================================================================================================================
@implementation PRDevice

static Byte deviceModel = PRDEVICE_UNKNOWN;

#pragma mark Model Info.
//---------------------------------------------------------------------------------------------------------------------
+ (Byte)getDeviceModel
{
	NSString *deviceModelName = [[UIDevice currentDevice] machine];
	
	if ([deviceModelName isEqualToString:@"i386"]) {

			CGFloat scaleFactor = 1.0f;
			if ([[UIScreen mainScreen] respondsToSelector:NSSelectorFromString(@"scale")]) {
				scaleFactor = [[UIScreen mainScreen] scale];
			}

			CGRect screenRect = [[UIScreen mainScreen] bounds];
			if (screenRect.size.width == 768 || screenRect.size.width == 1024) { deviceModel = PRDEVICE_IPAD; }
			if (screenRect.size.width == 640 || screenRect.size.width == 960) { deviceModel = PRDEVICE_IPHONE_4; }
			if (screenRect.size.width == 320 || screenRect.size.width == 480) {
				if (scaleFactor == 1.0f)  { deviceModel = PRDEVICE_IPHONE; }
				else { deviceModel = PRDEVICE_IPHONE_4; }
			}
			return deviceModel;
	}
	else if ([deviceModelName isEqualToString:@"iPhone1,1"]) {
		deviceModel = PRDEVICE_IPHONE;
	}
	else if ([deviceModelName isEqualToString:@"iPhone1,2"]) {
		deviceModel = PRDEVICE_IPHONE_3G;
	}
	else if ([deviceModelName isEqualToString:@"iPhone2,1"]) {
		deviceModel = PRDEVICE_IPHONE_3GS;	
	}
	else if ([deviceModelName isEqualToString:@"iPhone3,1"]) {
		deviceModel = PRDEVICE_IPHONE_4;
	}
	else if ([deviceModelName isEqualToString:@"iPod1,1"]) {
		deviceModel = PRDEVICE_IPOD;
	}
	else if ([deviceModelName isEqualToString:@"iPod2,1"]) {
		deviceModel = PRDEVICE_IPOD_2G;
	}
	else if ([deviceModelName isEqualToString:@"iPod2,2"]) {
		deviceModel = PRDEVICE_IPOD_2G2;
	}
	else if ([deviceModelName isEqualToString:@"iPod3,1"]) {
		deviceModel = PRDEVICE_IPOD_3G;
	}
	else if ([deviceModelName isEqualToString:@"iPod4,1"]) {
		deviceModel = PRDEVICE_IPOD_4G;
	}
	else if ([deviceModelName isEqualToString:@"iPad1,1"]) {
		deviceModel = PRDEVICE_IPAD;
	}
	else if ([deviceModelName isEqualToString:@"iPad1,1"]) {
		deviceModel = PRDEVICE_IPAD2;
	}
	return deviceModel;
}

//---------------------------------------------------------------------------------------------------------------------
+ (Byte)getDeviceType  {
	return (deviceModel & PRDEVICE_TYPE_MASK);
}

//---------------------------------------------------------------------------------------------------------------------
+ (BOOL)isiPad {
	Byte deviceType = [PRDevice getDeviceType];
	return ((deviceType & PRDEVICE_TYPE_IPAD) != 0);
}

//---------------------------------------------------------------------------------------------------------------------
+ (BOOL)isHD {
	if (deviceModel == PRDEVICE_TYPE_IPAD)  {return YES;}
	return [PRDevice isRetinaDisplay];
}

//---------------------------------------------------------------------------------------------------------------------
+ (BOOL)isRetinaDisplay {
	if (deviceModel == PRDEVICE_IPHONE_4)  {return YES;}
	if (deviceModel == PRDEVICE_IPOD_4G)  {return YES;}
	return NO;
}

#pragma mark Basic Info.
//---------------------------------------------------------------------------------------------------------------------
+ (NSString *)getUDID  {return [[UIDevice currentDevice] uniqueIdentifier];}

//---------------------------------------------------------------------------------------------------------------------
+ (NSString *)getDeviceName  {return [[UIDevice currentDevice] name];}

//---------------------------------------------------------------------------------------------------------------------
+ (NSString *)getSystemName  {return [[UIDevice currentDevice] systemName];}

//---------------------------------------------------------------------------------------------------------------------
+ (NSString *)getSystemVersion  {return [[UIDevice currentDevice] systemVersion];}

//---------------------------------------------------------------------------------------------------------------------
+ (NSString *)getDeviceModelName  {return [[UIDevice currentDevice] model];}

//---------------------------------------------------------------------------------------------------------------------
+ (NSString *)getSystemLanguage  {return [[NSLocale currentLocale] localeIdentifier];}

//---------------------------------------------------------------------------------------------------------------------
+ (NSString *)getDeviceLanguage  {
	NSArray *languageArray = [NSLocale preferredLanguages];
	NSString *currentLanguage = [languageArray objectAtIndex:0];
	return currentLanguage;
}

#pragma mark Cocoa Framework
//---------------------------------------------------------------------------------------------------------------------
+ (BOOL)isGameCenterAvailable {
	
	Class gcClass = (NSClassFromString(@"GKLocalPlayer"));
	
	//  GameCenter is available on iOS 4.1 or later
	NSString *reqSysVer = @"4.1";
	NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
	BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
	
	return (gcClass && osVersionSupported);
}
@end
